package com.kele.loc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author kele
 * @date 2021/10/9 16:14
 * @description
 */
public class LockSaleTicket {

    public static void main(String[] args) {

        Ticket ticket=new Ticket();
        new Thread(()->{
            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }

        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }
        },"B").start();

    }
}


class Ticket{
    public int num=30;
    Lock lock=new ReentrantLock();
    public  void sale(){
        lock.lock();
        try {
            if (num>0){
                num--;
                System.out.println(Thread.currentThread().getName()+" :正卖票 "+num+"剩余"+num );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }
}

